import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;

import static java.util.Arrays.stream;

class Transpose {

    private static String RightTrim(final String inputString) {
        int lastIndex = inputString.length() - 1;
        while ((lastIndex >= 0) && (inputString.charAt(lastIndex) == ' ')) lastIndex--;

        return inputString.substring(0, lastIndex + 1);
    }

    String transpose(final String inputString) {
        if (inputString.equals("")) return "";

        String[] inputStringArray = inputString.split("\n");
        Map<Integer, StringBuilder> results = new HashMap<>();
        int newLineMaxLength = stream(inputStringArray).max(Comparator.comparingInt(String::length)).get().length();


        stream(inputStringArray).forEach((String s) -> {
            String[] tmp = s.split("");
            IntStream.range(0, newLineMaxLength).forEach(i -> {
                if (results.containsKey(i))
                    results.get(i).append(i >= tmp.length ? " " : tmp[i]);
                else
                    results.put(i, (new StringBuilder()).append(i >= tmp.length ? " " : tmp[i]));
            });
        });


//        trim right whitespace with last sentence.
        results.put(results.size() - 1, new StringBuilder(
                Transpose.RightTrim(results.get(results.size() - 1).toString())
        ));

        results.keySet().forEach(key -> results.get(key).append("\n"));

        StringBuilder sb = new StringBuilder();
        results.keySet().forEach(key -> sb.append(results.get(key)));

        return sb.substring(0, sb.length() - 1);
    }
}